package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.*;
import com.sxhalo.fsms.exception.BaseEnumError;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.*;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SysDicCodeController extends BaseController {

	@Autowired
	private ISysDicCodeService codeService;

	@ResponseBody
	@RequestMapping(value = "/dict", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDic(Integer typeId,Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "delete_flag=0";
		if(typeId!=null&&typeId!=0){
			where+= " and type_id="+typeId;
		}
		String order = " update_time desc";
		if(!isBlank(req.getParameter("dictCode"))) {
			where += " and dict_code like '%" + req.getParameter("dictCode").trim() + "%'";
		}
		if (!isBlank(req.getParameter("dictValue"))) {
			where += " and dict_value like '%" + req.getParameter("dictValue").trim() + "%'";
		}
		if (!isBlank(req.getParameter("dictDescription"))) {
			where += " and dict_description like '%" + req.getParameter("dictDescription").trim() + "%'";
		}
		if (!isBlank(req.getParameter("deleteFlag"))) {
			where += " and delete_flag = " + req.getParameter("deleteFlag") ;
		}
		return findList(current, pageSize, where, order, codeService);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "新增字典", modelName = "字典管理")
	@RequestMapping(value = "/dict", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDic(@RequestBody  SysDicCode record,  HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setCreateTime(nowtime);
		record.setDeleteFlag(0);
		codeService.insertSelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "更新字典", modelName = "字典管理")
	@RequestMapping(value = "/dict", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDic(@RequestBody  SysDicCode record, String method, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		codeService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "字典管理")
	@RequestMapping(value = "/dict", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult delDic(String dictCode,Integer typeId, HttpServletRequest req) {
		SysDicCode record =new SysDicCode();
		record.setTypeId(typeId);
		record.setDictCode(dictCode);
		record.setDeleteFlag(1);
		codeService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "批量删除", modelName = "字典管理")
	@RequestMapping(value = "/dict/batchdel", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult batchDelDic(String ids,Integer typeId, HttpServletRequest req) {

		codeService.deleteByWhere(" dict_code in ('"+ids.replaceAll(",","','")+"') and type_id='"+typeId+"'");
		return ApiResult.retOK(req);
	}




	@ResponseBody
	@RequestMapping(value = "/dict/{type}", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDic(@PathVariable("type") String type, HttpServletRequest req) {
		if (type != null) {
			List<SysDicCode> list = codeService.findWhereList("type_id=" + type, "dict_code");
			return ApiResult.retOK(req, list);
		}
		return ApiResult.retOK(req);
	}




	@ResponseBody
	@RequestMapping(value = "/validDicCode", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult validCode(SysDicCodeKey key, HttpServletRequest req) {
		SysDicCode record = codeService.selectByPrimaryKey(key);
		if (record == null) {
			return ApiResult.retOK(req);
		}
		return ApiResult.retOK(req,BaseEnumError.数据重复);
	}



}
